<?php
namespace app\api\controller\wsgoods;

class Usershare extends Base{

    public function __construct() {
        parent::__construct();
        $this->checkLogin();
    }

    //我的分享
    public function lists(){
        $d = $this->post();
        $rule = [
            'type'=>'require|integer',
            'page'=>'require|integer',
            'limit'=>'require|integer',
        ];
        $msg = [
        ];
        check($rule,$msg,$d);

        $map = [];
        $map[] = ["and","a.user_id","=",$this->userinfo["id"]];
        $map[] = ["and","a.type","=",$d['type']];
        $map[] = ["and","b.status","=",0];
        $w = where($map);
        $order = "order by a.id desc";
        if($d['type'] == 0){
            $sql = "
                select
                    b.id as goods_id,
                    b.title goods_name,
                    b.pic,
                    b.price,
                    b.point,
                    b.goods_price_id,
                    b.description,
                    a.id as collect_id,
                    a.user_id
                from
                    user_share a
                inner join
                    goods b on b.goods_price_id = a.tab_id
                $w[0] {$order} ".limit($d['page'],$d['limit']);
        }else{
            $sql = "
                select
                    b.id as shop_id,
                    b.username shop_name,
                    b.pic,
                    b.phone,
                    b.introduce,
                    a.id as collect_id,
                    a.user_id
                from
                    user_share a
                inner join
                    user_ext b on b.id = a.tab_id
                $w[0] {$order} ".limit($d['page'],$d['limit']);
        }

        $rs = $this->db->query($sql,$w[1],'',true);
        suc($rs[0],$rs[1]);
    }

    //添加分享
    public function add(){
        $d = $this->post();
        $rule = [
            'id'=>'require',
            'type'=>'require|integer',
        ];
        $msg = [];
        check($rule,$msg,$d);
        $info = $this->db->query('select * from user_share where tab_id=? and user_id=? and type=?',[$d['id'],$this->userinfo["id"],$d['type']]);
        if(!$info){
            $data = [
                'tab_id' => $d['id'],
                'user_id' => $this->userinfo["id"],
                'type' => $d['type'],
                'create_time' => time(),
            ];
            $rs = $this->db->insert('user_share',$data);
            if (!$rs) {
                err('分享失败!');
            }
            if($d['type'] == 0){//用户获得积分
                $order_no = com_order_no();
                $mysql[] = $order_id = m('Orderjifen','c')->add($order_no,$this->userinfo["id"],$this->sys['share_point'],2);
                $mysql[] =m('Userbill','xcom')->doit($order_no,$this->userinfo["id"],105,'order_jifen',$order_id,$this->sys['share_point'],'money_jf','');
            }
        }
        suc();
    }

    //删除分享
    public function del(){
        $d = $this->post();
        $rule = [
            'id'=>'require',
            'type'=>'require|integer',
        ];
        $msg = [];
        check($rule,$msg,$d);
        $this->db->query('delete from user_share where tab_id=? and user_id=? and type=?',[$d['id'],$this->userinfo["id"],$d['type']]);
        suc("操作成功");
    }

}
?>
